<template>
	<view class="container" style="padding-bottom: 120rpx;">
		<uni-data-select
			v-model="selectMode"
			:localdata="modeList"
			@change="change"
			:clear="false"
			placeholder="请选择工作模式"
		></uni-data-select>
		<u--form ref="queryForm" :model="query">
			<u-form-item label="" prop="containerCode" style="display: flex;">
				<span style="margin-right: 20rpx;">容器码</span>
				<u--input 
					placeholder="请输入/扫描" 
					v-model="query.containerCode" 
					suffixIcon="scan" 
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
				>
				</u--input>
			</u-form-item>
			<u-form-item label="" prop="storageCode" style="display: flex;">
				<span style="margin-right: 20rpx;">储位码</span>
				<u--input 
					placeholder="请输入/扫描" 
					v-model="query.storageCode" 
					suffixIcon="scan" 
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
				>
				</u--input>
			</u-form-item>
			<u-form-item v-if="selectMode !== 1" label="" prop="destination" style="display: flex;">
				<span style="margin-right: 20rpx;">目的地</span>
				<u--input
					placeholder="请输入/扫描" 
					:disabled="!query.storageCode"
					v-model="query.destination" 
					suffixIcon="scan" 
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
				>
				</u--input>
			</u-form-item>
		</u--form>
		<view v-if="selectMode === 1" style="position: fixed; bottom: 0; left: 0; width: 100%; padding: 10rpx; background-color: #fff; display: flex; justify-content: space-between;box-sizing: border-box;gap: 20rpx;">
			<u-button type="primary" text="确认叫车" @click="submitCall"></u-button>
			<!-- <u-button type="error" text="料箱紧急入库" @click="emergency"></u-button> -->
			<u-button type="warning" text="重置" @click="reset"></u-button>
		</view>
		<view v-else style="position: fixed; bottom: 0; left: 0; width: 100%; padding: 10rpx; background-color: #fff; display: flex; justify-content: space-between;box-sizing: border-box;gap: 20rpx;">
			<u-button type="primary" text="确认叫车" @click="submitPointToPoint"></u-button>
			<u-button type="warning" text="重置" @click="reset"></u-button>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import uniDataSelect from '../../../node_modules/@dcloudio/uni-ui/lib/uni-data-select/uni-data-select.vue'
	import { checkStorageAPI, emergencyAPI, callInboundAPI, checkContainerAPI } from '@/api/work/callCar.js'
	export default {
		components: {
			uniDataSelect
		},
		data() {
			return {
				query: {
					containerCode: '',
					storageCode: '',
					destination: '',
					isPointToPoint: ''
				},
				// needDestination: true,
				selectMode: 1,
				modeList: [
					{
						value: 1,
						text: '扫码叫车'
					},
					{
						value: 2,
						text: '点对点叫车'
					}
				]
			};
		},
		onLoad(options) {
			uni.$on('scancodedate',(data) => this.getCode(data.code))
		},
		onUnload() {  
		  // 移除监听事件      
		  uni.$off('scancodedate')
		},
		methods: {
			getCode(code) {
				const pattern = /^[A-Za-z]\d{9}$/
				if (pattern.test(code)) {
					this.query.containerCode = code
					this.checkContainer()
				} else if (code.includes('-')) {
					if (this.selectMode === 1) {
						this.query.storageCode = code
					} else {
						if (!this.query.storageCode) {
							this.query.storageCode = code
							// this.checkStorage()
						} else if (this.query.storageCode) {
							this.query.destination = code
						}
					}
				}
			},
			reset() {
				this.query = {
					containerCode: '',
					storageCode: '',
					destination: '',
					isPointToPoint: ''
				}
				// this.needDestination = false
			},
			async checkStorage() {
				// const res = await checkStorageAPI(this.query.storageCode)
				// if (res.meta.code === 200) {
				// 	if (res.data.need === 1) {
				// 		this.needDestination = true
				// 		this.$refs.uToast.show({
				// 			type: 'success',
				// 			message: '该储位需要扫描/填写目的地码'
				// 		})
				// 	} else {
				// 		this.$refs.uToast.show({
				// 			type: 'warning',
				// 			message: '该储位不需要扫描/填写目的地码'
				// 		})
				// 	}
				// } else {
				// 	return this.$refs.uToast.show({
				// 		type: 'warning',
				// 		message: res.meta.msg
				// 	})
				// }
			},
			async checkContainer() {
				const res = await checkContainerAPI({
					containerCode: this.query.containerCode
				})
				if (res.meta.code !== 200) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.meta.msg
					})
				}
			},
			async bind() {
				const res = await callInboundAPI(this.query)
				if (res.meta.code === 200) {
					this.$refs.uToast.show({
						type: 'success',
						message: res.meta.msg
					})
					this.reset()
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.meta.msg
					})
				}
			},
			submitCall() {
				if (!this.query.containerCode) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描容器码'
					})
				}
				if (this.query.containerCode && !this.query.storageCode) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描储位码'
					})
				}
				this.query.isPointToPoint = 0
				this.bind()
			},
			async submitPointToPoint() {
				if (!this.query.containerCode) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描容器码'
					})
				}
				if (this.query.containerCode && !this.query.storageCode) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描储位码'
					})
				}
				if (this.query.containerCode && this.query.storageCode && !this.query.destination) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请目的储位码'
					})
				}
				this.query.isPointToPoint = 1
				this.bind()
			},
			async emergency() {
				const res = await emergencyAPI()
				if (res.meta.code === 200) {
					this.$refs.uToast.show({
						type: 'success',
						message: res.meta.msg
					})
					this.reset()
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.meta.msg
					})
				}
			},
			change() {
				this.reset()
			}
		}
	}
</script>

<style lang="scss">

</style>
